package gui;

import gameObjects.Terrain;

import java.awt.Dimension;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.ImageIcon;

import com.json.parsers.JSONParser;
import com.json.parsers.JsonParserFactory;


public class MapLoader {
	private MainWindow mainWindow;
	private Map jsonData ;

	private class JsonMap{
		int height;
		int width;
		int tileWidth;
		int tileHeight;
		ArrayList<ArrayList<Integer>> layersTilesNumbers = new ArrayList<ArrayList<Integer>>();
	}

	public MapLoader(MainWindow mainWindow){
		this.mainWindow=mainWindow;
	}

	public void loadMap(String mapFile,String encoding){
		try{
			FileInputStream inputStream= new FileInputStream(mapFile); 
			JsonParserFactory factory= JsonParserFactory.getInstance();
			JSONParser parser = factory.newJsonParser();
			jsonData = parser.parseJson(inputStream, encoding);
			JsonMap jsonMap=loadJsonMap();
			drawJsonMap(jsonMap);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
	}


	private void drawJsonMap(JsonMap jsonMap){
		int layerDepth = 1;

		for (ArrayList<Integer> layer:jsonMap.layersTilesNumbers){

			int i=0;
			for (int h=0 ; h<jsonMap.height;h++){
				for (int w=0 ; w<jsonMap.width;w++){

					int tileNo=(layer.get(i).intValue());
					if (tileNo!=0){
						ImageIcon icon= new ImageIcon("images\\"+tileNo+".png");
						Terrain terrain = new Terrain();
						terrain.setX(w);
						terrain.setY(h);
						if (layerDepth != 1){
							terrain.setWalkable(false);
						}


						GuiObject guiObject = new GuiObject(terrain,icon);
						guiObject.setBounds(w*jsonMap.tileWidth, h*jsonMap.tileHeight, jsonMap.tileWidth, jsonMap.tileHeight);
						guiObject.addMouseListener(mainWindow.mouseEventListener);
						mainWindow.layeredPane.add(guiObject);
						mainWindow.layeredPane.setLayer(guiObject, layerDepth);
					}
					i++;

				}
			}

			layerDepth += 2;

		}
		Dimension dim = new Dimension(jsonMap.width*jsonMap.tileWidth, jsonMap.height*jsonMap.tileHeight);
		mainWindow.layeredPane.setMaximumSize(dim);
		mainWindow.layeredPane.setMinimumSize(dim);
		mainWindow.layeredPane.setPreferredSize(dim);
		mainWindow.layeredPane.setSize(dim);
		mainWindow.layeredPane.repaint();
	}


	private JsonMap loadJsonMap(){
		JsonMap jsonMap= new JsonMap();

		jsonMap.height=Integer.parseInt((String)jsonData.get("height"));
		jsonMap.tileHeight=Integer.parseInt((String)jsonData.get("tileheight"));
		jsonMap.width=Integer.parseInt((String)jsonData.get("width"));
		jsonMap.tileWidth=Integer.parseInt((String)jsonData.get("tilewidth"));

		//ładowanie warstw
		List al=(List)jsonData.get("layers");

		for(Object obj:al){
			Map alMap=(Map)obj;
			ArrayList<String> tmp= (ArrayList<String>)alMap.get("data");
			ArrayList<Integer> tilesNumbers= new ArrayList<Integer>();
			for (String s:tmp){
				tilesNumbers.add(new Integer(s));
			}

			jsonMap.layersTilesNumbers.add(tilesNumbers);
		}

		return jsonMap;
	}
}
